{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "cade6057-9d48-4186-a4d4-90b76dcf3152",
   "metadata": {},
   "source": [
    "Chapter 09\n",
    "# 定义、使用父类 (动物)、子类 (鸡、兔、猪)\n",
    "Book_1《编程不难》 | 鸢尾花书：从加减乘除到机器学习  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "e093fd21-8419-4fd2-9db3-c8840f13ed83",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 父类，动物\n",
    "class Animal:\n",
    "    def __init__(self, name, age):\n",
    "        self.name = name\n",
    "        self.age = age\n",
    "\n",
    "    def eat(self):\n",
    "        print(f\"{self.name} is eating.\")\n",
    "\n",
    "    def sleep(self):\n",
    "        print(f\"{self.name} is sleeping.\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "5b8836eb-9f10-48bc-a076-f9492b801fb1",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 子类，鸡\n",
    "class Chicken(Animal):\n",
    "    def __init__(self, name, age, color):\n",
    "        super().__init__(name, age)\n",
    "        self.color = color\n",
    "\n",
    "    def lay_egg(self):\n",
    "        print(f\"{self.name} is laying an egg.\")\n",
    "   "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "4e28a3db-0ccf-4522-bc61-53ddfc81cbdb",
   "metadata": {},
   "outputs": [],
   "source": [
    "     \n",
    "# 子类，兔\n",
    "class Rabbit(Animal):\n",
    "    def __init__(self, name, age, speed):\n",
    "        super().__init__(name, age)\n",
    "        self.speed = speed\n",
    "\n",
    "    def jump(self):\n",
    "        print(f\"{self.name} is jumping.\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "d0131e75-12a3-4ee6-8018-b3b61776af8a",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 子类，猪\n",
    "class Pig(Animal):\n",
    "    def __init__(self, name, age, weight):\n",
    "        super().__init__(name, age)\n",
    "        self.weight = weight\n",
    "\n",
    "    def roll(self):\n",
    "        print(f\"{self.name} is rolling around.\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "c5480937-5f26-46fd-84e5-b7b013133635",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "chicken1 is eating.\n",
      "chicken1 is laying an egg.\n"
     ]
    }
   ],
   "source": [
    "chicken1 = Chicken(\"chicken1\", 1, \"white\")\n",
    "chicken1.eat(); chicken1.lay_egg()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "0d755110-fc63-4139-96b1-bf1f50b5f33c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "rabbit1 is sleeping.\n",
      "rabbit1 is jumping.\n"
     ]
    }
   ],
   "source": [
    "rabbit1 = Rabbit(\"rabbit1\", 2, 10)\n",
    "rabbit1.sleep(); rabbit1.jump()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "869373a5-25af-4799-8e71-7330f9906b25",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "pig1 is eating.\n",
      "pig1 is rolling around.\n"
     ]
    }
   ],
   "source": [
    "pig1 = Pig(\"pig1\", 3, 100)\n",
    "pig1.eat(); pig1.roll()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
